package controllers;
import static play.data.Form.form;
import java.util.List;
import com.avaje.ebean.Page;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import controllers.TableRequest.StringPair;
import models.Example;
import play.data.Form;
import play.libs.Json;
import play.mvc.*;
import models.Driver;
public class Examples extends Controller{
    public static Result search() {
        Form<TableRequest> reqform= form(TableRequest.class).bindFromRequest();
            TableRequest req = null;
                if (reqform.hasErrors()) {
                    return internalServerError(InternalErrorMsg.paramError(reqform.errorsAsJson().toString()));
                } else {
                    req = reqform.get();
                }
            List<Example> all = null;
        try {
            int pageIndex = req.iDisplayStart / req.iDisplayLength;
            StringPair sort = req.getSortPair();
            Page<Example> page = Example.page(pageIndex, req.iDisplayLength, sort.str1, sort.str2, req.getSearchColums(), req.getSearchStrings());
            all = page.getList();
            } catch (Exception e) {
                return internalServerError(InternalErrorMsg.getError(e.getMessage()));
        }
        TableResponse<Example> resp = new TableResponse<Example>();
        resp.iTotalDisplayRecords = all.size();
        resp.iTotalRecords = all.size();
        resp.aaData = all;
        resp.sEcho = req.sEcho;
        return ok(Json.toJson(resp));
    }
    public static Result add() {
        Form<Example> aForm = form(Example.class).bindFromRequest();
        Example aModel = null;
        if (aForm.hasErrors()) {
            return internalServerError(InternalErrorMsg.paramError(aForm.errorsAsJson().toString()));
        } 
        try {
            aModel = aForm.get();
            aModel.setDriver(Driver.findByDrive_licence_type(aModel.getDriver().getDrive_licence_type()));
            aModel.save();
        } catch (Exception e) {
            return internalServerError(InternalErrorMsg.getError(e.getMessage()));
        }
        return ok(Json.toJson(aModel));
    }
    public static Result delete() {
        Form<DeleteRequest> f = form(DeleteRequest.class).bindFromRequest();
        if (f.hasErrors()) {
            return internalServerError(InternalErrorMsg.paramError(f.errorsAsJson().toString()));
        } 
        try {
            Example.find.byId(f.get().id).delete();
        } catch (Exception e) {
            return internalServerError(InternalErrorMsg.getError(e.getMessage()));
        }
        return ok("ok");
    }
    public static Result update() {
        Form<UpdateRequest> f = form(UpdateRequest.class).bindFromRequest();
        if (f.hasErrors()) {
            return internalServerError(InternalErrorMsg.getError(f.errorsAsJson().toString()));
        } 
        try {
            UpdateRequest r = f.get();
            Example aModel = Example.find.byId(r.id);
            String[] fields = r.columnName.split("\\.");
            if (fields.length == 1) {
                BeanRefUtil.updateField(aModel, r.columnName, r.value);
            } else if (fields.length == 2) {
                    if (fields[0].equalsIgnoreCase("driver")) {
                        aModel.setDriver(Driver.findByDrive_licence_type(r.value));
                    }
            }
            aModel.update();
            return ok(r.value);
        } catch (Exception e) {
            return internalServerError(InternalErrorMsg.getError(e.getMessage()));
        }
    }
    public static Result select() {
        List<Example> all = Example.find.all();
        ObjectNode node = Json.newObject();
        for(Example v: all) {
            node.put(v.id.toString(), v.getId()); 
        }
        return ok(node.toString());
    }
}
